package com.bos.controller.json;

import com.bos.dao.RoleDAO;
import com.bos.domain.Role;
import com.bos.editor.RoleEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class RoleJsonController {

    private RoleDAO roleDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setRoleDAO(RoleDAO roleDAO) {
        this.roleDAO = roleDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/role.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Role> index() {
        try {
            return roleDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Role>();
        }
    }

    @RequestMapping(value = "/role/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Role show(@PathVariable("id") Role role) {
        return role;
    }

    @RequestMapping(value = "/role.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Role role) {
        if (role == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            roleDAO.save(role);
            result.put("success", true);
            result.put("role", role);
            result.put("message", "Sukses Save Role " + role);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("role", role);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Role " + role);
        }

        return result;
    }

    @RequestMapping(value = "/role/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Role role) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            roleDAO.delete(role);
            result.put("success", true);
            result.put("message", "Sukses Delete Role " + role);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Role " + role);
        }
        return result;
    }

    @RequestMapping(value = "/role.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Role role) {
        if (role == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            roleDAO.merge(role);
            result.put("success", true);
            result.put("role", role);
            result.put("message", "Sukses Update Role " + role);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("role", role);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal update role " + role);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Role.class, new RoleEditor(roleDAO));
    }

}
